<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="../js/jquery-1.11.3.js"></script>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      .main {
        border: 1px solid white;
        width: 100%;
        height: 500px;
      }
      .login-form {
        position: relative;
        border: 1px solid white;
        width: 300px;
        height: 320px;
        margin: 80px 0 40px 900px;
        background-color: rgba(236, 233, 233, 0.904);
      }
      .login-tab {
        font-size: 30px;
        text-align: center;
        margin-top: 30px;
        color: red;
      }
      .login-txt {
        margin-bottom: 20px;
      }
      .from {
        width: 300px;
        height: 180px;
        padding: 10px;
        margin-top: 20px;
      }
      label {
        display: inline-block;
        width: 50px;
        text-align: center;
        font-size: 14px;
      }
      input {
        height: 20px;
        outline: none;
        padding: 5px 10px;
        font-size: 14px;
      }
      .login-btn {
        width: 270px;
        margin-top: 20px;
      }
      .login-btn button {
        cursor: pointer;
        border: none;
        width: 100%;
        height: 35px;
        background-color: red;
        color: white;
        text-align: center;
        line-height: 30px;
        font-size: 20px;
      }
      .tz {
        position: absolute;
        bottom: 20px;
        text-decoration: none;
        font-size: 12px;
        right: 0;
        color: red;
      }
      .errol-promot {
        margin-left: 60px;
        font-size: 12px;
        color: red;
        display: block;
        display: none;
      }
      .login_logo {
        width: 1200px;
        height: 60px;
        margin: 0 auto;
      }
      .login_logo a {
        vertical-align: middle;
        float: left;
      }
      .login_logo h1 {
        margin-top: 10px;
        float: left;
        height: 60px;
        line-height: 60px;
        vertical-align: middle;
        margin-left: 20px;
        font-size: 26px;
        color: #666;
      }
      .login-banner {
        width: 100%;
        height: 475px;
        background-color: #e93854;
        position: absolute;
        left: 0;
        top: 70px;
      }
      .l_pic {
        height: 100%;
        position: absolute;
        width: 990px;
        left: 300px;
        background: url(https://passport.jd.com/new/misc/2015/background.png);
        background-position: 0 0;
        background-repeat: no-repeat;
      }
    </style>
  </head>
  <body>
    <div class="main">
      <div class="login_logo">
        <a>
          <img
            src="https://misc.360buyimg.com/lib/img/e/logo-201305-b.png"
            alt=""
          />
        </a>
        <h1>欢迎注册</h1>
      </div>
      <div class="login-banner">
        <div class="l_pic"></div>
      </div>
      <div class="login-form">
        <div class="login-tab">注册成为京东会员</div>
        <form class="from" onsubmit="return false">
          <div class="login-txt">
            <label for="">用户名:</label>
            <input type="text" placeholder="用户名" id="txt" name="username" />
            <span class="errol-promot"></span>
          </div>
          <div class="login-psw">
            <label for="">密码:</label>
            <input
              type="password"
              placeholder="密码"
              id="psw"
              name="password"
            />
            <span class="errol-promot"></span>
          </div>
          <div class="login-btn">
            <button id="btn">注&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;册</button>
          </div>
        </form>
        <a href="login.html" class="tz">已有账号去登陆</a>
      </div>
    </div>
    <script>
      $(function () {
        let a = false;
        let b = false;
        $("#txt").blur(function (e) {
          //console.log($("#txt").val());
          let str = /^[a-z0-9_-]{3,16}$/;
          if ($("#txt").val() == "") {
            $(".login-txt>.errol-promot").show().text("用户名不能为空");
            a = false;
            return;
          } else {
            a = true;
          }
          if (!str.test($("#txt").val())) {
            $(".login-txt>.errol-promot").show().text("用户名格式不正确");
            a = false;
            return;
          } else {
            $(".login-txt>.errol-promot").hide();
            a = true;
          }
        });
        $("#psw").blur(function (e) {
          //console.log($("#psw").val());
          let str1 = /^[a-z0-9_-]{6,18}$/;
          if ($("#psw").val() == "") {
            $(".login-psw>.errol-promot").show().text("密码不能为空");
            b = false;
            return;
          } else {
            b = true;
          }
          if (!str1.test($("#psw").val())) {
            $(".login-psw>.errol-promot").show().text("密码格式不正确");
            b = false;
            return;
          } else {
            $(".login-psw>.errol-promot").hide();
            b = true;
          }
        });
        $("#btn").on("click", function (e) {
          if (a == true && b == true) {
            let data = $("#txt").val();
            let psw = $("#psw").val();
            $.get(
              "http://jx.xuzhixiang.top/ap/api/reg.php",
              {
                username: data,
                password: psw,
              },
              (res) => {
                if (res.code === 0) {
                  alert("该用户已存在");
                  return;
                } else {
                  alert("注册成功");
                  location.href = "./login.html";
                }
                //console.log(res);
              }
            );
            //console.log(data, psw);
          } else {
            alert("请输入正确的格式");
          }
        });
      });
    </script>
  </body>
</html>
